#!/bin/bash

# ------------------------------ Procedures ----------------------------
readFile ()
{
FILE=$1
if [ -f $FILE ]; then
  cat $FILE
else
  printf "-"
fi
}

showCleanupSettings ()
{
  echo "<H3>Database cleanup settings for domain $SIM_MANDOMAIN.</H3>"
  if [ ! -r $SIM_CLEANDBCONF ]; then
    $SIM_SUDO $SIM_UPDSETTINGS makeCleanDb
    echo "Created database cleanup settings file.<BR>"
  else 
    echo ".$SIM_CLEANDBCONF."
  fi
  if [ ! -f $SIM_CLEANDBCONF ]; then
    echo "Still no configfile found for this utility ($SIM_CLEANDBCONF).<BR>"
  else
    cat $SIM_CLEANDBCONF | while read PARAM VALUE 
    do
      case $PARAM in
        (cleanOldAfter) oldcleanOldAfter=$VALUE ;;
        (cleanDownAfter) oldcleanDownAfter=$VALUE ;;
	(cleanDynAfter) oldcleanDynAfter=$VALUE ;;
	(cleanResAfter) oldcleanResAfter=$VALUE ;;
	(cleanAutoAfter) oldcleanAutoAfter=$VALUE ;;
	(remMonNoSnmp)  oldremMonNoSnmp=$VALUE ;;
        (---end---)     echo "Config read....<BR>" 
	  # Default settings.
	  if [ "$oldcleanOldAfter" = "" ]; then
	    oldcleanOldAfter=5
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam cleanOldAfter $oldcleanOldAfter
	  fi
	  if [ "$oldcleanDownAfter" = "" ]; then
	    oldcleanDownAfter=60
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam cleanDownAfter $oldcleanDownAfter
          fi
	  if [ "$oldcleanDynAfter" = "" ]; then
	    oldcleanDynAfter=60
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam cleanDynAfter $oldcleanDynAfter
	  fi
	  if [ "$oldcleanResAfter" = "" ]; then
	    oldcleanResAfter=60
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam cleanResAfter $oldcleanResAfter
	  fi
	  if [ "$oldcleanAutoAfter" = "" ]; then
	    oldcleanAutoAfter=60
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam cleanAutoAfter $oldcleanAutoAfter
	  fi
	  if [ "$oldremMonNoSnmp" = "" ]; then
	    oldremMonNoSnmp=no
	    $SIM_SUDO $SIM_UPDSETTINGS addCleandbParam remMonNoSnmp $oldremMonNoSnmp
	  fi
	  echo "Current settings:
	    <TABLE>
       	    <FORM method=post Action=$SCRIPT_NAME?$MODULE+$MODE+settings+update-cleanup>
	    <TR><TD COLSPAN=2 BGCOLOR=#BBBBBB>Settings for normal operations</TD></TR>
	    <TR><TD>Remove Old entries after:</TD><TD><INPUT NAME=cleanOldAfter SIZE=3 VALUE=$oldcleanOldAfter>days</TD></TR>
	    <TR><TD>Remove Down entries after:</TD><TD><INPUT NAME=cleanDownAfter SIZE=3 VALUE=$oldcleanDownAfter>days</TD></TR>
	    <TR><TD>Remove Dyn entries after:</TD><TD><INPUT NAME=cleanDynAfter SIZE=3 VALUE=$oldcleanDynAfter>days</TD></TR>
	    <TR><TD>Remove Res entries after:</TD><TD><INPUT NAME=cleanResAfter SIZE=3 VALUE=$oldcleanResAfter>days</TD></TR>
	    <TR><TD>Remove Auto entries after:</TD><TD><INPUT NAME=cleanAutoAfter SIZE=3 VALUE=$oldcleanAutoAfter>days</TD></TR>
	    <TR><TD COLSPAN=2 BGCOLOR=#BBBBBB>Settings for test environments</TD></TR>
	    <TR><TD>Remove mon nodes not responding to snmp:</TD><TD>
	    <SELECT NAME=remMonNoSnmp size=1>
		      <OPTION VALUE=$oldremMonNoSnmp>$oldremMonNoSnmp</OPTION>
		      <OPTION VALUE=no>no</OPTION>
		      <OPTION VALUE=yes>yes</OPTION>
	    </SELECT>
	    </TD></TR>
	    </TABLE>
	    <INPUT TYPE=submit Value=Update>
	    </FORM>
	       " ;;
	(*)             echo "Oops, $PARAM=$VALUE not recognized." ;;
      esac
    done
  fi
}

updCleanup ()
{
  echo "<PRE>
        Changing settings for the database cleanup."
  cat $SIM_CLEANDBCONF | while read PARAM VALUE
  do
    echo "PARAM=$PARAM VALUE=$VALUE"
    case $PARAM in
     (cleanOldAfter)  
               if [ "$VALUE" != "$FORM_cleanOldAfter" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam cleanOldAfter $NUM $FORM_cleanOldAfter
	       fi ;;
     (cleanDownAfter)  
               if [ "$VALUE" != "$FORM_cleanDownAfter" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam cleanDownAfter $NUM $FORM_cleanDownAfter
	       fi ;;
     (cleanDynAfter)  
               if [ "$VALUE" != "$FORM_cleanDynAfter" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam cleanDynAfter $NUM $FORM_cleanDynAfter
	       fi ;;
     (cleanResAfter)  
               if [ "$VALUE" != "$FORM_cleanResAfter" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam cleanResAfter $NUM $FORM_cleanResAfter
	       fi ;;
     (cleanAutoAfter)  
               if [ "$VALUE" != "$FORM_cleanAutoAfter" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam cleanAutoAfter $NUM $FORM_cleanAutoAfter
	       fi ;;
     (remMonNoSnmp)  
               if [ "$VALUE" != "$FORM_remMonNoSnmp" ]; then
                 $SIM_SUDO $SIM_UPDSETTINGS updCleandbParam remMonNoSnmp $NUM $FORM_remMonNoSnmp
	       fi ;;
    esac
  done
  echo "</PRE>"
  showCleanupSettings
  
}

setDispAccess ()
{
  TOOL=$1
  ACTION=$2
  RIGHT=$FORM_selectDispAccess
  ACCESS_FILE=$SIM_ACCESSD/$TOOL+$ACTION
  echo "$SIM_ACCESSD/$TOOL+$ACTION. to $FORM_selectDispAccess"
  echo "$RIGHT" > $ACCESS_FILE
  echo "Accesslevel for tool $TOOL, with action $ACTION, set to `cat $ACCESS_FILE`."

}

adminSettings ()
{
  SETTINGSFILE=$SIM_ACCESSLEVELD
  if [ -f $SETTINGSFILE ]; then
    ACCESS_SETTING=`cat $SETTINGSFILE`
  else
    ACCESS_SETTING=no
  fi
  echo "Currrent access setting set to $ACCESS_SETTING.
        <A HREF=$SCRIPT_NAME?$MODULE+$MODE+setUpdLevel>Modify</A>"
}

setUpdLevel ()
{
  SETTINGSFILE=$SIM_ACCESSLEVELD
  if [ -f $SETTINGSFILE ]; then
    rm $SETTINGSFILE
    echo "Display of access-levels deactivated."
  else
    echo "yes" > $SETTINGSFILE
    echo "Display of access-levels activated."
  fi
  echo "<BR>"
  adminSettings

}

userSettings ()
{
  if [ -d $SIM_SETTINGSD ]; then
    if [ -f $SIM_SETTINGSD/lessColors-$REMOTE_USER ]; then
      FULLCOLORS=no
    else
      FULLCOLORS=yes
    fi
    echo "Display Full color lines ($FULLCOLORS)
          <A HREF=$SCRIPT_NAME?$MODULE+$MODE+updLessColors>Modify</A>"
  else
    echo "Oops, no settings dir found...."
  fi
}

updLessColors ()
{
  if [ -f $SIM_SETTINGSD/lessColors-$REMOTE_USER ]; then
    echo "Turning to full color."
    rm $SIM_SETTINGSD/lessColors-$REMOTE_USER
  else
    echo "Turning to less colors"
    touch $SIM_SETTINGSD/lessColors-$REMOTE_USER
  fi
  echo "<BR>"
  userSettings
}

# ------------------------------- Main --------------------------------
                                                                                                                             

if [ "$1" = "" ]; then
  MODULE=none
else
  MODULE=$1
fi
if [ "$2" = "" ]; then
  MODE=none
else
  MODE=$2
fi

if [ "$3" = "" ] || [ "$3" = "none" ]; then
  ACTION=$MODE
else
  ACTION=$3
fi
if [ "$4" = "" ]; then
  PARAM1=none
else
  PARAM1=$4
fi
if [ "$5" = "" ]; then
  PARAM2=none
else
  PARAM2=$5
fi

#echo "MODULE=$MODULE, MODE=$MODE, ACTION=$ACTION, PARAM1=$PARAM1, PARAM2=$PARAM2.<BR>"
if [ "$MODE" = "adminSettings" ]; then
  echo "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0><TR><TD BGCOLOR=#EEEEEE><CENTER>
      <A HREF=$SCRIPT_NAME?$MODULE+$MODE+userSettings>User settings</A> -
      <A HREF=$SCRIPT_NAME?$MODULE+$MODE+adminSettings>Admin settings</A> -
      <A HREF=$SCRIPT_NAME?$MODULE+$MODE+cleanupdb>Db Cleanup</A> 
      </TD></TR></TABLE><CENTER>
     "
else
  echo "<CENTER>"
fi
case $ACTION in
  (cleanupdb)   showCleanupSettings ;;
  (update-cleanup) updCleanup ;;
  (setDispAccess)  setDispAccess $PARAM1 $PARAM2 ;;
  (adminSettings)  adminSettings ;;
  (userSettings)   userSettings ;;
  (updLessColors)  updLessColors ;;
  (setUpdLevel)    setUpdLevel ;;
  (*) echo "Please select an action from the menu......" ;;
esac
